﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Entity;
using System.Data;

namespace BLL
{
    public class GroupsService
    {
        GroupsDAL groupDAL = new GroupsDAL();
        ParticipantsDAL participantsDAL = new ParticipantsDAL();
        
        public void CreateGroup(BoGroups groups) 
        {
            groups.GRO_ID = System.Guid.NewGuid().ToString();
            groupDAL.Insert(groups);
        }
        /// <summary>
        /// 插入一行数据，并返回该数据的小组id
        /// </summary>
        /// <param name="groups">小组对象</param>
        /// <returns>返回小组id</returns>
        public string InsertGroup(BoGroups groups)
        {
            groups.GRO_ID = System.Guid.NewGuid().ToString();
            groupDAL.Insert(groups);
            return groups.GRO_ID;
        }
        
        public void UpdateGroups(BoGroups groups)
        {
            groupDAL.Update(groups);

            IList<BoParticipants> pars = participantsDAL.GetGroupParticipants(groups.GRO_ID);
            foreach (BoParticipants par in pars)
            {
                par.PAR_AWARDS = groups.GRO_AWARDS;
                participantsDAL.Update(par);
            }
        }
       
        public bool DeleteGroups(BoGroups groups)
        {
            return groupDAL.Delete(groups.GRO_ID)>0;
        }

        /// <summary>
        /// 根据竞赛的id来判断是否有此小组名称
        /// </summary>
        /// <param name="groupName">小组名称</param>
        /// <param name="ConId">竞赛id</param>
        /// <returns>返回bool类型</returns>
        public bool CheckGroupName(string groupName, string ConId)
        {
            return groupDAL.CheckGroupName(groupName, ConId);
        }

        public BoGroups GetGroupByName(string groupName, string contestId)
        {
            return groupDAL.GetGroupByName(groupName, contestId);
        }

        /// <summary>
        /// 根据条件来获得相应的数据的行数
        /// </summary>
        /// <param name="constr"></param>
        /// <returns></returns>
        public int getCountByCon(string constr)
        {
            return groupDAL.getCountByCon(constr);
        }
        public void DeleteGroup(BoGroups group)
        {
            groupDAL.Delete(group.GRO_ID);
        }
        public BoGroups getGroups(string id)
        {
            return groupDAL.GetModel(id);
        }
        public IList<BoGroups> getAllGroups()
        {
            return groupDAL.GetAllGroups();
        }
        public IList<BoGroups> getContestGroups(string contestId)
        {
            return groupDAL.GetContestGroups(contestId);
        }
        /// <summary>
        /// 通过竞赛id以及小组名称获取视图的信息
        /// </summary>
        /// <param name="contestId">竞赛id</param>
        /// <param name="GroupName">小组名称</param>
        /// <returns></returns>
        public DataTable Getv_participantStuAndGroup(string contestId,string GroupName)
        {
            return groupDAL.Getv_participantStuAndGroup(contestId, GroupName);
        }

        
    }
}
